package com.zh.modules.app.interceptor;


import com.zh.common.exception.ServiceException;
import com.zh.common.utils.Constants;
import com.zh.modules.app.utils.JwtUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 权限(Token)验证
 */
@Component
public class AuthorizationInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = JwtUtils.getToken();
        if (StringUtils.isEmpty(token)) {
            return true;
//            throw new ServiceException("未登录，请重新登录", HttpStatus.FORBIDDEN.value());
        }
        if (!JwtUtils.verify(token)) {
            throw new ServiceException("登录失效，请重新登录", HttpStatus.FORBIDDEN.value());
        }
        return true;
    }
}
